package com.ayoka.parenting.widget;

import android.os.Bundle;
import android.support.annotation.ColorInt;
import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.ayoka.parenting.R;
import com.jincp.common.brvah.BaseQuickAdapter;
import com.jincp.common.brvah.BaseViewHolder;
import com.jincp.common.brvah.RecycleViewDivider;
import com.jincp.common.brvah.layoutmanager.MyLinearLayoutManager;
import com.jincp.common.dialog.BaseDialogBuilder;
import com.jincp.common.dialog.BaseDialogFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by jinchunping on 2019/1/23.
 */

public class SelectorItemDialog extends BaseDialogFragment {

    public static int SINGLE_MODE = 0;
    public static int MULTI_MODE = 1;

    public static SelectorItemDialog newInstance(SelectorItemDialog.Builder builder) {
        SelectorItemDialog dialog = new SelectorItemDialog();
        Bundle bundle = getArgumentBundle(builder);
        dialog.setArguments(bundle);
        dialog.setLayoutResource(builder.layoutResource);
        dialog.setSelectorItemList(builder.selectorItemList);
        dialog.setSelectorMode(builder.selectorMode);
        dialog.setConvertSelectorItemView(builder.convertSelectorItemView);
        return dialog;
    }

    private int layoutResource = R.layout.item_selector_item;
    private List<SelectorItemBean> selectorItemList;
    private int selectorMode = MULTI_MODE;
    private ConvertSelectorItemView convertSelectorItemView;

    @Override
    protected void convertView(@Nullable View container, final BaseDialogFragment dialogFragment) {
        RecyclerView recyclerView = container.findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new MyLinearLayoutManager(getActivity()));
        SelectorItemAdapter adapter = new SelectorItemAdapter(layoutResource, selectorItemList, selectorMode, convertSelectorItemView);
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (selectorMode == MULTI_MODE) {
                    ((SelectorItemAdapter)adapter).setMultiChoice(position);
                } else if (selectorMode == SINGLE_MODE) {
                    ((SelectorItemAdapter)adapter).setSingleChoice(position);
                }
            }
        });
        recyclerView.setAdapter(adapter);
        recyclerView.addItemDecoration(RecycleViewDivider.getLineDivider(getActivity()));

        container.findViewById(R.id.tv_cancel).setOnClickListener(new onCloseClickListener());
        container.findViewById(R.id.tv_sure).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismissAllowingStateLoss();
                if (mDialogResultListener != null) {
                    mDialogResultListener.result(SelectorItemDialog.this, selectorItemList);
                }
            }
        });
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_selector;
    }

    public void setLayoutResource(int layoutResource) {
        if (layoutResource != 0) {
            this.layoutResource = layoutResource;
        }
    }

    public void setSelectorItemList(List<SelectorItemBean> selectorItemList) {
        this.selectorItemList = selectorItemList;
    }

    public void setSelectorMode(int selectorMode) {
        this.selectorMode = selectorMode;
    }

    public void setConvertSelectorItemView(ConvertSelectorItemView convertSelectorItemView) {
        this.convertSelectorItemView = convertSelectorItemView;
    }

    public static class Builder extends BaseDialogBuilder<Builder, SelectorItemDialog> {

        private int layoutResource;
        private List<SelectorItemBean> selectorItemList;
        private int selectorMode;

        private ConvertSelectorItemView convertSelectorItemView;

        public Builder setLayoutResource(@LayoutRes int layoutResource) {
            this.layoutResource = layoutResource;
            return this;
        }

        public Builder setSelectorItemList(List<SelectorItemBean> selectorItemList) {
            this.selectorItemList = selectorItemList == null ? new ArrayList<SelectorItemBean>() : selectorItemList;
            return this;
        }

        public Builder setSelectorMode(int selectorMode) {
            this.selectorMode = selectorMode;
            return this;
        }

        public Builder setConvertSelectorItemView(ConvertSelectorItemView convertSelectorItemView) {
            this.convertSelectorItemView = convertSelectorItemView;
            return this;
        }


        @Override
        public SelectorItemDialog build() {
            return SelectorItemDialog.newInstance(this);
        }
    }

    /**
     * 点击右边关闭dialog监听
     */
    private class onCloseClickListener implements View.OnClickListener{

        @Override
        public void onClick(View view) {
            dismiss();
        }
    }

    public interface SelectorItemBean{

        String getItemText();

        @ColorInt
        int getItemTextColor();

        boolean isSelected();

        void setSelected(boolean isSelected);

        Object getItemValue();
    }

    private static class SelectorItemAdapter extends BaseQuickAdapter<SelectorItemBean, BaseViewHolder> {

        private ConvertSelectorItemView convertSelectorItemView;
        private int selectorMode;

        public SelectorItemAdapter(int layoutResource, List<SelectorItemBean> selectorItemList, int selectorMode, ConvertSelectorItemView convertSelectorItemView) {
            super(layoutResource, selectorItemList);
            this.convertSelectorItemView = convertSelectorItemView;
            this.selectorMode = selectorMode;
        }

        @Override
        protected void convert(BaseViewHolder holder, SelectorItemBean item) {
            if (convertSelectorItemView != null) {
                convertSelectorItemView.onConvertSelectorItemView(holder, item);
            } else {
                holder.setText(R.id.tv_selector_item, item.getItemText())
                        .setTextColor(R.id.tv_selector_item, item.getItemTextColor())
                        .setSelected(R.id.tv_selector_item, item.isSelected());
                TextView textView = holder.getView(R.id.tv_selector_item);
                if (selectorMode == SINGLE_MODE) {
                    textView.setCompoundDrawablesWithIntrinsicBounds(0,0,R.drawable.selector_radio_button_primary_with_hook,0);
                } else if (selectorMode == MULTI_MODE) {
                    textView.setCompoundDrawablesWithIntrinsicBounds(0,0,R.drawable.selector_check_box_primary_with_hook,0);
                }
            }
        }

        public void setSingleChoice(int position) {
            for (int i = 0; i < getData().size(); i++) {
                if (getItem(i) == null) continue;
                getItem(i).setSelected(i == position);
            }
            notifyDataSetChanged();
        }

        public void setMultiChoice(int position) {
            SelectorItemBean selectorItemBean = getItem(position);
            selectorItemBean.setSelected(!selectorItemBean.isSelected());
            notifyItemChanged(position);
        }
    }

    public interface ConvertSelectorItemView{
        void onConvertSelectorItemView(BaseViewHolder holder, SelectorItemBean item);
    }
}